package com.cn.goodsadmin.util;


import com.cn.goodsadmin.common.frame.NetService;
import com.cn.goodsadmin.common.frame.RequestInfo;
import com.cn.goodsadmin.config.BaseConfig;
import com.cn.goodsadmin.config.CgiConfig;
import com.cn.goodsadmin.po.*;
import com.cn.goodsadmin.requestinfo.GetAcessTokenAns;
import com.cn.goodsadmin.requestinfo.OrderStatusRequestInfo;

public class SendTempUtil {

    public static void sendOrderUpdateTips(Object o, Template template, String appid, Logistics logistics) {
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.url = BaseConfig.getacessUrl;
        requestInfo.appId = appid;
        requestInfo.requestKey = CgiConfig.GETACESSTOKEN;
        GetAcessTokenAns acessTokenAns = (GetAcessTokenAns) NetService.getInstance().startSynHttpNetWork(requestInfo);
        if (!acessTokenAns.isError) {
            String aceessthoken = acessTokenAns.aceesToken;
            System.out.println(aceessthoken);
            OrderStatusRequestInfo orderStatusRequestInfo = coverRequstInfo(aceessthoken, o, logistics, template);
            NetService.getInstance().startAsyHttpNetWork(orderStatusRequestInfo);
        }
    }


    private static OrderStatusRequestInfo coverRequstInfo(String aceessthoken, Object o, Logistics logistics, Template template) {
        OrderStatusRequestInfo orderStatusRequestInfo = new OrderStatusRequestInfo();
        if (o instanceof OrderInfo) {
            OrderInfo orderInfo = (OrderInfo) o;
            orderStatusRequestInfo.callurl = BaseConfig.baseurl + "?openid=" + orderInfo.getcOpenid() + "&orderid=" + orderInfo.getcOrderid() + "&appid=" + orderInfo.getcAppid()
                    + "&lid=" + logistics.getcLogisticsid();
            orderStatusRequestInfo.orderid = orderInfo.getcOrderid();
            orderStatusRequestInfo.openid = orderInfo.getcOpenid();
            orderStatusRequestInfo.orderStatuts = orderInfo.getcStatus();
            orderStatusRequestInfo.logisticsid = orderInfo.getcLogisticsid();
        }
        else if (o instanceof Userreal) {
            Userreal userreal = (Userreal) o;
            orderStatusRequestInfo.callurl = BaseConfig.baseurl + "?openid=" + userreal.getcOpenid() + "&orderid=" + userreal.getcId() + "&appid=" + userreal.getcAppid()
                    + "&lid=" + logistics.getcLogisticsid();
            orderStatusRequestInfo.orderid = String.valueOf(userreal.getcId())+"  商品:"+userreal.getcRealname();
            orderStatusRequestInfo.openid = userreal.getcOpenid();
            orderStatusRequestInfo.orderStatuts = userreal.getcIsuse();
            orderStatusRequestInfo.logisticsid = userreal.getcLogisticsid();
        }
        else if(o instanceof PanicLog){
            PanicLog panicLog=(PanicLog)o;
            orderStatusRequestInfo.callurl = BaseConfig.baseurl + "?openid=" + panicLog.getcOpenid() + "&orderid=" + panicLog.getcId() + "&appid=" + panicLog.getcAppid()
                    + "&lid=" + logistics.getcLogisticsid();
            orderStatusRequestInfo.orderid = String.valueOf(panicLog.getcPid())+"  商品:"+panicLog.getcGname();
            orderStatusRequestInfo.openid = panicLog.getcOpenid();
            orderStatusRequestInfo.orderStatuts = panicLog.getcStatus();
            orderStatusRequestInfo.logisticsid = panicLog.getcLogisticsid();
        }
        orderStatusRequestInfo.requestKey = CgiConfig.WEIXINORDERSTATUTS;
        orderStatusRequestInfo.template_id = template.getcTemplateId();
        orderStatusRequestInfo.url = BaseConfig.WXSENDTEMPURL + aceessthoken;
        orderStatusRequestInfo.remark = creatLogisticsInfo(logistics);
        return orderStatusRequestInfo;
    }


    private static String creatLogisticsInfo(Logistics logistics) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("物流公司:").append(logistics.getcName());
        buffer.append(",").append("运单号:").append(logistics.getcLogisticsid());
        return buffer.toString();
    }

}
